using System.Collections.Generic;
using UnityEngine;

public class RandomRoomManager:MonoBehaviour,ISaveRandomManager
{
    public static RandomRoomManager instance;
    private void Awake()
    {
        instance=this;
    }
    [Header("存储标识")]
    public int saveId = 1;
    [Header("雾")]
    public GameObject fog;
    [Header("门数据")]
    public List<GameObject> doorPre = new List<GameObject>();
    [Header("生成敌人数据")]
    public List<GameObject> enemyPre = new List<GameObject>();
    [Header("道具数据")]
    public List<GameObject> itemPre = new List<GameObject>();
    [Header("类型")]
    public RoomType type;
    [Header("随机敌人所有位置")]
    public List<Transform> allEnemyPos = new List<Transform>();
    [Header("随机道具所有位置")]
    public List<Transform> allChooseItemPos = new List<Transform>();
    [Header("商店道具所有位置")]
    public List<Transform> allShopItemPos = new List<Transform>();
    [Header("门位置")]
    public Transform doorPos;
    [Header("三选一提示")]
    public GameObject chooseTip;

    //临时对象
    public RandomData data;
    public GameObject door;
    //生成的数据
    public List<EnemyData> enemyDatas = new List<EnemyData>();
    public List<GameObject> itemDatas = new List<GameObject>();

    //保存的数据,这里敌人数据可以公用，道具也可以公用
    public List<bool> saveItem = new List<bool>();
    public List<bool> isBuy = new List<bool>();
    public List<string> saveItemName = new List<string>();
    public List<int> itemCoinData = new List<int>();
    public List<Vector2> enemyPos = new List<Vector2>();
    public bool isOpen = false;
    public string doorName;
    public bool isChoose = false;

    private void Start()
    {
        LoadData();
        StartCoroutine(Init());
    }
    //加载数据
    public void LoadData()
    {
        //加载Random数据
        SaveManager.instance.LoadRandomData(Application.persistentDataPath, "Data" + GameManager.Instance.fileNum + "/RandomData"+saveId);
        SaveManager.instance.SeekRandomDataInterface();
        SaveManager.instance.GetRandomInterface();

        data=SaveManager.instance.randomData;
    }
    //保存数据
    public void SaveData()
    {
        //保存Random数据
        SaveManager.instance.SeekRandomDataInterface();
        SaveManager.instance.SaveRandomInterface();
        SaveManager.instance.SaveRandomData(Application.persistentDataPath, "Data"+GameManager.Instance.fileNum+"/RandomData"+saveId);
    }
    //初始化
    public System.Collections.IEnumerator Init()
    {
        yield return new WaitForSeconds(0.1f);
        //加载数据
        LoadData();
        fog.SetActive(true);
        chooseTip.SetActive(false);
        //如果已经进行了随机
        if (data!=null&&data.isRandom)
        {
            OverRandomEvent();
            //设置door
            SetDoor();
        }
        else
        {
            RandomEvent();
            //创建门
            CreateDoor();
        }
    }
    //处理已经随机的事件
    public void OverRandomEvent()
    {
        if(type==RoomType.Enemy)
        {
            SetEnemy();
        }
        else if(type==RoomType.Choose)
        {
            SetItem();
        }
        else if(type==RoomType.Shop)
        {
            SetShopItem();
        }
    }
    //处理没有随机的事件
    public void RandomEvent()
    {
        //起始为30%敌人，30%道具，40%商店
        int enemyNum = 100+GameManager.Instance.shopItemRoomNum*20-GameManager.Instance.enemyRoomNum*20+GameManager.Instance.itemRoomNum*20;
        int itemNum = 75+GameManager.Instance.shopItemRoomNum*20+GameManager.Instance.enemyRoomNum*20-GameManager.Instance.itemRoomNum*20;
        int shopNum = 50-GameManager.Instance.shopItemRoomNum*20+GameManager.Instance.enemyRoomNum*20+GameManager.Instance.itemRoomNum*20;

        int allNum = enemyNum;
        int randomX = Random.Range(0, allNum);
        if(randomX<shopNum)
        {
            CreateShopItemRoom();
            type=RoomType.Shop;
            GameManager.Instance.shopItemRoomNum+=1;
        }
        else if(randomX<itemNum)
        {
            CreateItemRoom();
            type=RoomType.Choose;
            GameManager.Instance.itemRoomNum+=1;
        }
        else
        {
            CreateEnemy();
            type=RoomType.Enemy;
            GameManager.Instance.enemyRoomNum+=1;
        }
    }
    //这里是第一次进行随机
    //创建敌人房间
    public void CreateEnemy()
    {
        //随机选择敌人，先无boss，3-8个敌人
        int randomX = Random.Range(5, 12);
        List<Transform> currPos = new List<Transform>();
        foreach(var i in allEnemyPos)
        {
            currPos.Add(i);
        }

        for(int i=0;i<randomX;i++)
        {
            GameObject newObj = Instantiate(enemyPre[Random.Range(0, enemyPre.Count)]);

            //设置位置
            int index = Random.Range(0, currPos.Count);
            newObj.transform.SetParent(transform);
            newObj.transform.position=currPos[index].position;
            currPos.RemoveAt(index);
            //添加敌人
            enemyDatas.Add(newObj.GetComponent<Enemy>().data);
            enemyPos.Add(newObj.transform.position);

            SceneItemManager.instance.SetEnemy(newObj.transform);

            Debug.Log(newObj);
        }
    }
    //创建商店道具房间
    public void CreateShopItemRoom()
    {
        List<GameObject> myList = new List<GameObject>();
        foreach(var i in itemPre)
        {
            myList.Add(i);
        }
        for (int i = 0; i<allChooseItemPos.Count; i++)
        {
            int index = Random.Range(0, myList.Count);
            //随机选择一个道具
            GameObject newObj = Instantiate(myList[index]);
            myList.RemoveAt(index);
            //随机价钱5-30
            int currCoin = Random.Range(5, 30);
            //添加数据
            isBuy.Add(false);
            itemCoinData.Add(currCoin);
            saveItemName.Add(newObj.GetComponent<ItemModify>().myName);
            newObj.transform.SetParent(transform, false);
            itemDatas.Add(newObj);
            //设置
            newObj.GetComponent<ItemModify>().SetRandomRoom(currCoin);
            //设置位置
            newObj.transform.position=allShopItemPos[i].position;
            //设置权重
            SceneItemManager.instance.SetWeight(newObj.transform, 3);
        }
    }
    //创建道具房间
    public void CreateItemRoom()
    {
        chooseTip.SetActive(true);
        for(int i=0;i<allChooseItemPos.Count;i++)
        {
            //随机选择一个道具
            GameObject newObj = Instantiate(itemPre[Random.Range(0,itemPre.Count)]);
            //添加数据
            saveItemName.Add(newObj.GetComponent<ItemModify>().myName);
            newObj.GetComponent<ItemModify>().isChoose=true;
            newObj.transform.SetParent(transform, false);
            itemDatas.Add(newObj);
            //设置位置
            newObj.transform.position=allChooseItemPos[i].position;
            //设置权重
            SceneItemManager.instance.SetWeight(newObj.transform,3);
        }
    }
    //生成门
    public void CreateDoor()
    {
        GameObject newObj = Instantiate(doorPre[Random.Range(0, doorPre.Count)]);
        newObj.transform.SetParent(transform);
        newObj.transform.position=doorPos.position;
        newObj.GetComponent<Door>().isRandomDoor=true;
        door=newObj;
        doorName=door.GetComponent<Door>().color;

        //设置门为ENemy
        SceneItemManager.instance.SetEnemy(newObj.transform);
    }
    //这里是有数据了，只进行赋值设计
    //设置敌人
    public void SetEnemy()
    {
        //根据生成的敌人，遍历生成敌人
        for (int i=0;i<enemyDatas.Count;i++)
        {
            if (enemyDatas[i].isDie) continue;
            for (int j=0;j<enemyPre.Count;j++)
            {
                if (enemyDatas[i].myName==enemyPre[j].GetComponent<Enemy>().data.myName)
                {
                    GameObject newObj = Instantiate(enemyPre[j]);

                    //设置位置
                    newObj.transform.SetParent(transform);
                    newObj.transform.position=enemyPos[i];
                    newObj.GetComponent<Enemy>().data=enemyDatas[i];

                    SceneItemManager.instance.SetEnemy(newObj.transform);
                }
            }
        }
    }
    //设置道具
    public void SetItem()
    {
        if(isChoose==false)
        {
            chooseTip.SetActive(true);
            //只有没有选择才会打开
            for (int i=0;i<saveItemName.Count;i++)
            {
                //开始遍历相同名称生成
                for (int j = 0; j<itemPre.Count; j++)
                {
                    //名称相同，生成
                    if (saveItemName[i]==itemPre[j].GetComponent<ItemModify>().myName)
                    {
                        //随机选择一个道具
                        GameObject newObj = Instantiate(itemPre[j]);
                        //添加数据
                        newObj.GetComponent<ItemModify>().isChoose=true;
                        newObj.transform.SetParent(transform, false);
                        itemDatas.Add(newObj);
                        //设置位置
                        newObj.transform.position=allChooseItemPos[i].position;
                        //设置权重
                        SceneItemManager.instance.SetWeight(newObj.transform, 3);

                        break;
                    }
                }
            }

        }
    }
    //设置商店道具
    public void SetShopItem()
    {
        for (int i = 0; i<allChooseItemPos.Count; i++)
        {
            if (isBuy[i]) continue;
            for(int j=0;j<itemPre.Count;j++)
            {
                if (itemPre[j].GetComponent<ItemModify>().myName==saveItemName[i])
                {
                    //随机选择一个道具
                    GameObject newObj = Instantiate(itemPre[j]);
                    //随机价钱5-30
                    int currCoin = itemCoinData[i];
                    //添加数据
                    itemDatas.Add(newObj);
                    newObj.transform.SetParent(transform, false);
                    //设置
                    newObj.GetComponent<ItemModify>().SetRandomRoom(currCoin);
                    //设置位置
                    newObj.transform.position=allShopItemPos[i].position;
                    //设置权重
                    SceneItemManager.instance.SetWeight(newObj.transform, 3);
                    break;
                }
            }
        }
    }
    //关掉所有道具
    public void CloseAllItem()
    {
        foreach(var i in itemDatas)
        {
            i.SetActive(false);
        }
        isChoose=true;
        chooseTip.SetActive(false);
    }
    //关掉特定道具
    public void CloseItem(GameObject item)
    {
        item.SetActive(false);
        //寻找到开关然后关掉
        for(int i=0;i<itemDatas.Count;i++)
        {
            if (item.GetComponent<ItemModify>().myName==saveItemName[i])
            {
                isBuy[i]=true;
                SceneItemManager.instance.SetWeight(item.transform, 0);
            }
        }
    }
    //设置门
    public void SetDoor()
    {
        //查看是否打开
        if (isOpen==false)
        {
            //生成对应颜色门
            for (int i = 0; i<doorPre.Count; i++)
            {
                if (doorPre[i].GetComponent<Door>().color==doorName)
                {
                    GameObject newObj = Instantiate(doorPre[i]);
                    newObj.transform.SetParent(transform);
                    newObj.transform.position=doorPos.position;
                    newObj.GetComponent<Door>().isRandomDoor=true;
                    door=newObj;

                    //设置门为ENemy
                    SceneItemManager.instance.SetEnemy(newObj.transform);

                    break;
                }
            }
        }
        else
        {
            //雾消失
            fog.SetActive(false);
        }
    }
    //门消失
    public void OpenDoor()
    {
        //可以行走
        SceneItemManager.instance.SetRun(door.transform);
        fog.SetActive(false);
        door.SetActive(false);
        isOpen=true;
    }
    public void Save(ref RandomData data)
    {
        if(data==null)
        {
            data=new RandomData();
        }

        //敌人数据
        data.enemyDatas=enemyDatas;
        data.enemyPos=enemyPos;
        //商店数据
        data.buyName=saveItemName;
        data.coinNum=itemCoinData;
        data.isBuy=saveItem;
        data.isBuy=isBuy;
        //道具
        data.chooseName=saveItemName;
        data.isChoose=isChoose;
        //门数据
        data.doorName=doorName;
        //通用设置
        data.isRandom=true;
        data.isOpne=isOpen;
        data.type=type;
    }
    public void Load(RandomData data)
    {
        if(data!=null)
        {
            //敌人数据
            enemyDatas=data.enemyDatas;
            enemyPos=data.enemyPos;
            //商店数据
            saveItemName=data.buyName;
            itemCoinData=data.coinNum;
            isBuy=data.isBuy;
            //道具
            saveItemName=data.chooseName;
            isChoose=data.isChoose;
            //门数据
            doorName=data.doorName;
            //通用设置
            isOpen=data.isOpne;
            type=data.type;
        }
    }
}